from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field


class QuoteData(BaseModel):
    """行情数据模型"""
    symbol: str = Field(..., description="股票代码")
    price: float = Field(..., description="当前价格")
    open: float = Field(0.0, description="开盘价")
    high: float = Field(0.0, description="最高价")
    low: float = Field(0.0, description="最低价")
    close: float = Field(0.0, description="收盘价")
    volume: int = Field(0, description="成交量")
    change: float = Field(0.0, description="涨跌额")
    change_percent: float = Field(0.0, description="涨跌幅")
    timestamp: datetime = Field(default_factory=datetime.now, description="时间戳")
    
    class Config:
        json_encoders = {
            datetime: lambda v: v.isoformat()
        }


class SubscribeRequest(BaseModel):
    """订阅请求模型"""
    action: str = Field(..., description="操作类型: subscribe/unsubscribe")
    symbols: list[str] = Field(..., description="股票代码列表")


class ApiResponse(BaseModel):
    """API响应模型"""
    status: str = Field(..., description="响应状态: success/error")
    message: str = Field("", description="响应消息")
    data: Optional[dict] = Field(None, description="响应数据")
    timestamp: datetime = Field(default_factory=datetime.now, description="响应时间")


class QuoteSubscription(BaseModel):
    """订阅信息模型"""
    client_id: str = Field(..., description="客户端ID")
    symbols: list[str] = Field(..., description="订阅的股票代码")
    created_at: datetime = Field(default_factory=datetime.now, description="创建时间")


class ServerStats(BaseModel):
    """服务器统计信息模型"""
    quote_count: int = Field(0, description="行情数据数量")
    client_count: int = Field(0, description="连接客户端数")
    subscription_count: int = Field(0, description="订阅关系数量")
    uptime: str = Field("", description="运行时间")
    memory_usage: float = Field(0.0, description="内存使用率")
    cpu_usage: float = Field(0.0, description="CPU使用率") 